<?php

/**
 * alltosun.com 订单发送短信及推送方法
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2016-1-25 下午8:04:56 $
 * $Id: $
 */
class message_widget
{

    /**
     * 市场人员调价
     */
    public function edit_goods_price_message($cur_user_info, $store_info, $marketing_info, $dealer_info) {
        //         if (ONDEV) {
        //             return false;
        //         }

        $role_id = user_helper::get_user_role_id($cur_user_info['id']);
        if ($role_id != user_config::MARKETING) {
            return false;
        }
        $replace_arr = array(
            'store_name' => $store_info['title'] . $store_info['title_branch'],
            'name' => $marketing_info['realname']
        );
        //发给合伙人短信
        aliysms_helper::sms_send('抱抱堂', $replace_arr, $dealer_info['mobile'], 'marketing_modify_price_dealer');
        //发推送消息以及站内信
        message_center_helper::get_config_add_message('marketing_modify_price_dealer', $replace_arr, $dealer_info['id'], 'manage', '', 0, 'arrival');
    }


    /**
     * 订单发货
     * @param unknown_type $is_agency
     * @param unknown_type $order_info
     * @param unknown_type $store_info
     * @param unknown_type $marketing_info
     * @param unknown_type $agency_info
     */
    public function transport_order_message($is_agency, $order_info, $store_info, $marketing_info, $agency_info, $person_info = array()) {
        //         if (ONDEV) {
        //             return false;
        //         }

        $order_id = $order_info['id'];

        $store_mobile = $store_info['mobile'];
        $store_title = $store_info['title'];
        $store_user_id = $store_info['user_id'];
        if (isset($marketing_info['mobile']) && $marketing_info) {
            $market_mobile = $marketing_info['mobile'];
            $market_name = $marketing_info['marketing_name'];
        }

        if (isset($person_info['other_assigned_name']) && $person_info['other_assigned_name']) {
            $other_assigned_name = $person_info['other_assigned_name'];
            $other_assigned_mobile = $person_info['other_assigned_mobile'];
        }

        $agency_mobile = $agency_info['mobile'];
        $agency_user_id = $agency_info['user_id'];
        $dealer_info = user_helper::get_user_info($agency_user_id);
        $agency_name = $dealer_info['realname'];

        if ($is_agency) {
            // 合伙人送货(或者是合伙人指派制定人员送货)
            if (isset($other_assigned_name) && $other_assigned_name) {
                // 指定人员
                // 是否有签约的市场人员(发给签约的市场人员)
                if ($marketing_info && isset($market_mobile) && $market_mobile && !$other_assigned_mobile) {
                    $replace_arr = array(
                        'marketing_name' => $market_name,
                        'store_name' => $store_title,
                        'people_name' => $other_assigned_name,
                        'phone_num' => $other_assigned_mobile
                    );
                    aliysms_helper::sms_send('抱抱堂', $replace_arr, $market_mobile, 'store_stock_person_marketing');
                    // 发推送消息以及站内信
                    message_center_helper::get_config_add_message('store_stock_person_marketing', $replace_arr, $store_user_id, 'manage', $order_id, 1, 'arrival');
                }

                //                 // 通知指定人员

                //                 // 发给店主
                //                 $replace_arr = array(
                //                     'store_name' => $store_title,
                //                     'order_id'   => $order_id,
                //                     'name'       => $other_assigned_name,
                //                     'phone_num'  => $other_assigned_mobile
                //                 );

                //                 aliysms_helper::sms_send('抱抱堂', $replace_arr, $store_mobile, 'person_delivery_store');
                //                 // 发推送消息以及站内信
                //                 message_center_helper::get_config_add_message('person_delivery_store', $replace_arr, $store_user_id, 'store', $order_id, 1, 'arrival');

            } else {
                $replace_arr = array(
                    'order_id' => $order_id,
                    'phone_num' => $agency_mobile,
                    'store_name' => $store_title,
                    'name' => $agency_name
                );
                aliysms_helper::sms_send('抱抱堂', $replace_arr, $store_mobile, 'dealer_delivery_store');
                // 发推送消息以及站内信
                message_center_helper::get_config_add_message('dealer_delivery_store', $replace_arr, $store_user_id, 'store', $order_id, 1, 'arrival');

            }

        } else {

            // 市场人员送货 (判断是否是其他市场人员送货)
            $assign_user_info = user_helper::get_user_info($order_info['assigned_id']);
            $default_marketing_info = user_helper::get_user_info($order_info['marketing_id']);

            if ($default_marketing_info['mobile'] == $assign_user_info['mobile']) {
                // 默认市场人员
                $sms_store_res_name = 'store_confirm';
                $sms_dealer_res_name = 'marketing_confirm_dealer';
            } else {
                // 市场人员b
                $replace_arr['name'] = $assign_user_info['realname'];
                $sms_store_res_name = 'marketing_b_confirm_store';
                $sms_dealer_res_name = 'marketing_b_confirm_dealer';
            }

            // 发给收货人
            $replace_arr['order_id'] = $order_id;
            $replace_arr['phone_num'] = $market_mobile;
            $replace_arr['store_name'] = $store_title;

            aliysms_helper::sms_send('抱抱堂', $replace_arr, $order_info['mobile'], $sms_store_res_name);

            //发推送消息以及站内信
            message_center_helper::get_config_add_message($sms_store_res_name, $replace_arr, $store_user_id, 'store', $order_id, 1, 'arrival');

            // 发给合伙人短信
            $replace_arr['order_id'] = $order_id;
            $replace_arr['store_name'] = $store_title;
            $replace_arr['phone_num'] = $market_mobile;

            aliysms_helper::sms_send('抱抱堂', $replace_arr, $agency_mobile, $sms_dealer_res_name);
            //发推送消息以及站内信
            message_center_helper::get_config_add_message($sms_dealer_res_name, $replace_arr, $agency_user_id, 'manage', $order_id, 1, 'arrival');
        }

    }


    /**
     * 确认送达
     */
    public function confirm_finished_message($cur_user_info, $store_info, $marketing_info, $dealer_info, $person_info, $order_id) {
        //         if (ONDEV) {
        //             return false;
        //         }

        // 店铺信息
        $store_title = $store_info['title'] . $store_info['title_branch'];

        // 总店信息
        $store_owner_info = user_helper::get_user_info($store_info['owner_id']);
        // 分店信息
        $store_user_info = user_helper::get_user_info($store_info['user_id']);
        if (!$store_owner_info || !$store_user_info) {
            return false;
        }
        $store_owner_phone_num = $store_owner_info['mobile'];
        $store_user_phone_num = $store_user_info['mobile'];

        // 市场人员信息
        $marketing_phone_num = '';
        if ($marketing_info) {
            $marketing_phone_num = $marketing_info['mobile'];
        } else {
            write_log(json_encode($marketing_info));
        }

        // 合伙人信息
        $dealer_phone_num = $dealer_info['mobile'];

        $msg_arr = array(
            'store_name' => $store_title,
            'order_id' => $order_id,
            'dealer_phone_num' => $dealer_phone_num,
            'marketing_phone_num' => $marketing_phone_num,
            'store_phone_num' => $store_user_phone_num,
            'phone_num' => $cur_user_info['mobile']
        );

        // 判断是否指派第三方人员
        if (isset($person_info['other_assigned_name']) && $person_info['other_assigned_name']) {
            // 发给第三方人员
            $other_assigned_name = $person_info['other_assigned_name'];
            $other_assigned_mobile = $person_info['other_assigned_mobile'];

            $role_id = 0;
        } else {
            // 角色
            $role_id = user_helper::get_user_role_id($cur_user_info['id']);
        }

        // 合伙人确认需要发给市场人员和商户
        if (user_config::AGENCY == $role_id) {

            // 判断是否是指定人员送达
            if ($person_info) {
                // 发给总店
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_phone_num, 'person_confirm_delivery_finished_store');
                message_center_helper::get_config_add_message('person_confirm_delivery_finished_store', $msg_arr, $store_info['id'], 'store', $order_id, 1, 'arrival');
            } else {
                // 发给总店
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_phone_num, 'marketing_confirm_delivery_finished_store');
                message_center_helper::get_config_add_message('marketing_confirm_delivery_finished_store', $msg_arr, $store_info['id'], 'store', $order_id, 1, 'arrival');
            }

            // 判断是否有市场人员
            // if ($marketing_info['id'] != $dealer_info['id']) {

            // }

        } elseif (user_config::MARKETING == $role_id) {

            // 判断是否是其他市场人员(发给合伙人的可共用)
            $decode_order_id = order_helper::order_id_decode($order_id);
            $order_info = order_helper::get_order_info($decode_order_id);
            $assign_marketing_info = user_helper::get_user_info($order_info['assigned_id']);
            $default_marketing_info = user_helper::get_user_info($order_info['marketing_id']);
            if ($default_marketing_info == $assign_marketing_info) {
                // 是默认市场人员
                // 发给总店
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_phone_num, 'marketing_confirm_delivery_finished_store');
                message_center_helper::get_config_add_message('marketing_confirm_delivery_finished_store', $msg_arr, $store_info['id'], 'store', $order_id, 1, 'arrival');
            } else {
                // 其他市场人员
                // 发给总店
                $msg_arr['name'] = $assign_marketing_info['realname'];
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_phone_num, 'marketing_b_confirm_finish_store');
                message_center_helper::get_config_add_message('marketing_b_confirm_finish_store', $msg_arr, $store_info['id'], 'store', $order_id, 1, 'arrival');
            }

            // 发给合伙人
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_phone_num, 'dealer_delivery_finished_dealer');
            message_center_helper::get_config_add_message('dealer_delivery_finished_dealer', $msg_arr, $dealer_info['id'], 'manage', $order_id, 1, 'arrival');


        } elseif (user_config::STORE_MANAGER == $role_id || user_config::STORE_OWNER == $role_id) {

            $have_marketing = false;
            // 判断是否有市场人员
            if (isset($marketing_info['id']) && $marketing_info['id'] && $marketing_info['id'] != $dealer_info['id']) {
                $have_marketing = true;
                $msg_arr['name'] = $marketing_info['realname'];
                $msg_arr['phone_num'] = $store_owner_phone_num;

                aliysms_helper::sms_send('抱抱堂', $msg_arr, $marketing_phone_num, 'store_confirm_delivery_finished_marketing');
                message_center_helper::get_config_add_message('store_confirm_delivery_finished_marketing', $msg_arr, $marketing_info['id'], 'manage', $order_id, 1, 'arrival');
            }

            // 发给合伙人(判断是否是指定人员)
            // '合伙人您好，${store_name}订单：${order_id}，已经确认到货，如有疑问请联系：第三方配送人员 ${phone_num}。',
            if (isset($other_assigned_mobile) && $other_assigned_mobile) {

                // 发给总店 
                $msg_arr['phone_num'] = $dealer_phone_num;
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_phone_num, 'person_confirm_delivery_finished_store');
                message_center_helper::get_config_add_message('person_confirm_delivery_finished_store', $msg_arr, $store_info['id'], 'store', $order_id, 1, 'arrival');

                // 发给合伙人
                $msg_arr['phone_num'] = $store_owner_phone_num;
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_phone_num, 'store_stock_store_confirm_dealer');
                message_center_helper::get_config_add_message('store_stock_store_confirm_dealer', $msg_arr, $dealer_info['id'], 'manage', $order_id, 1, 'arrival');
            } else {

                if ($have_marketing) {
                    // 发给合伙人
                    aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_phone_num, 'dealer_delivery_finished_dealer');
                    message_center_helper::get_config_add_message('dealer_delivery_finished_dealer', $msg_arr, $dealer_info['id'], 'manage', $order_id, 1, 'arrival');
                } else {
                    // 发给合伙人 没有市场人员
                    aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_phone_num, 'store_stock_store_confirm_dealer');
                    message_center_helper::get_config_add_message('store_stock_store_confirm_dealer', $msg_arr, $dealer_info['id'], 'manage', $order_id, 1, 'arrival');
                }

                // 发给店长
                $msg_arr['phone_num'] = $dealer_phone_num;
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_phone_num, 'marketing_confirm_delivery_finished_store');
                message_center_helper::get_config_add_message('marketing_confirm_delivery_finished_store', $msg_arr, $store_info['id'], 'store', $order_id, 1, 'arrival');
            }

        } else {
            // 第三方人员确认送达(发给商家)
            // person_confirm_delivery_finished_store
            $msg_arr['phone_num'] = $dealer_phone_num;
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_phone_num, 'person_confirm_delivery_finished_store');
            message_center_helper::get_config_add_message('person_confirm_delivery_finished_store', $msg_arr, $store_info['id'], 'store', $order_id, 1, 'arrival');

            // 发给合伙人
            if ($cur_user_info) {
                $msg_arr['phone_num'] = $store_user_phone_num;
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_phone_num, 'store_stock_store_confirm_dealer');
                message_center_helper::get_config_add_message('store_stock_store_confirm_dealer', $msg_arr, $dealer_info['id'], 'manage', $order_id, 1, 'arrival');
            } else {
                $msg_arr['phone_num'] = $other_assigned_mobile;
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_phone_num, 'person_delivery_finished_dealer');
                message_center_helper::get_config_add_message('person_delivery_finished_dealer', $msg_arr, $dealer_info['id'], 'manage', $order_id, 1, 'arrival');
            }


        }

    }

    /**
     * 撤销订单是发送短信及推送
     *
     * 发给商户总店合伙人和市场人员
     * @param $store_info 店铺信息(内有总店店长信息)
     * @param $marketing_info 市场人员信息
     * @param $dealer_info 合伙人信息
     * @param $person_info 第三方人员信息
     * @param $order_info  订单信息
     */

    public function cancel_order($cur_user_info, $store_info, $marketing_info, $dealer_info, $person_info, $order_info) {
        //         if (ONDEV) {
        //             return false;
        //         }

        // 店铺信息
        $store_name = $store_info['name'];
        $store_mobile = $store_info['mobile'];
        $store_title = $store_info['title'] . $store_info['title_branch'];

        // 市场人员信息
        if ($marketing_info) {
            $marketing_name = $marketing_info['name'];
            $marketing_mobile = $marketing_info['mobile'];;
        }

        // 合伙人信息
        $dealer_name = $dealer_info['name'];
        $dealer_mobile = $dealer_info['mobile'];

        // 第三方人员信息
        if ($person_info) {
            $person_name = $person_info['name'];
            $person_mobile = $person_info['mobile'];
        }

        //  订单信息
        $order_id = $order_info['order_id'];

        // 先发总店
        $msg_arr = array(
            'order_id' => $order_id,
            'store_name' => $store_title
        );

        $role_id = user_helper::get_user_role_id($cur_user_info['id']);

        // 合伙人撤销订单
        if (user_config::AGENCY == $role_id) {
            // 发给合伙人自己
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $cur_user_info['mobile'], 'dealer_cancel');
            message_center_helper::get_config_add_message('dealer_cancel', $msg_arr, $cur_user_info['id'], 'manage', '', 0);

            // 发给市场人员
            if (isset($store_info['marketing_id']) && $store_info['marketing_id'] && $marketing_info) {
                $msg_arr['name'] = $marketing_info['realname'];
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $marketing_mobile, 'store_cancel_marketing');
                message_center_helper::get_config_add_message('store_cancel_marketing', $msg_arr, $marketing_info['id'], 'manage', '', 0);
            }

            // 发给商户
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_mobile, 'store_cancel_store');
            message_center_helper::get_config_add_message('store_cancel_store', $msg_arr, $store_info['id'], 'store', '', 0);

            // 市场人员撤销订单
        } elseif (user_config::MARKETING == $role_id) {
            // 发给合伙人
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_mobile, 'dealer_cancel');
            message_center_helper::get_config_add_message('dealer_cancel', $msg_arr, $dealer_info['id'], 'manage', '', 0);

            // 发给店长
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_mobile, 'store_cancel_store');
            message_center_helper::get_config_add_message('store_cancel_store', $msg_arr, $store_info['id'], 'store', '', 0);

            // 发给自己
            $msg_arr['name'] = $cur_user_info['realname'];
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $cur_user_info['mobile'], 'store_cancel_marketing');
            message_center_helper::get_config_add_message('store_cancel_marketing', $msg_arr, $cur_user_info['id'], 'manage', '', 0);

            // 门店撤销
        } elseif (user_config::STORE_MANAGER == $role_id || user_config::STORE_OWNER) {
            //             if (user_config::STORE_OWNER == $role_id) {
            //                 $store_mobile = $cur_user_info['mobile'];
            //             } else {
            //                 $store_owner_info = user_helper::get_user_info($store_info['owner_id']);
            //                 $store_mobile = $store_owner_info['mobile'];
            //             }
            // 发给店长
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_mobile, 'store_cancel_store');
            message_center_helper::get_config_add_message('store_cancel_store', $msg_arr, $store_info['id'], 'store', '', 0);

            // 发给市场人员
            if (isset($store_info['marketing_id']) && $store_info['marketing_id'] && $marketing_info) {
                $msg_arr['name'] = $marketing_info['name'];
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $marketing_mobile, 'store_cancel_marketing');
                message_center_helper::get_config_add_message('store_cancel_marketing', $msg_arr, $marketing_info['id'], 'manage', '', 0);
            }

            // 发给合伙人
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_mobile, 'dealer_cancel');
            message_center_helper::get_config_add_message('dealer_cancel', $msg_arr, $dealer_info['id'], 'manage', '', 0);

        }


        //         // 总店发短信
        //         aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_mobile, 'store_cancel_store');

        //         // 发送推送
        //         message_center_helper::get_config_add_message('store_cancel_store', $msg_arr, $store_info['id'], 'store', '', 0);

        //         // 发给合伙人
        //         if ($dealer_info['id']) {
        //             aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_mobile, 'dealer_cancel');
        //             // 发送推送
        //             message_center_helper::get_config_add_message('dealer_cancel', $msg_arr, $dealer_info['id'], 'manage', '', 0);

        //         }

        //         // 发给市场人员
        //         if (isset($marketing_info['id']) && $marketing_info['id']) {
        //             $msg_arr['name'] = $marketing_info['realname'];
        //             aliysms_helper::sms_send('抱抱堂', $msg_arr, $marketing_mobile, 'store_cancel_marketing');
        //             // 发送推送
        //             message_center_helper::get_config_add_message('store_cancel_marketing', $msg_arr, $marketing_info['id'], 'manage', '', 0);

        //         }

        //         // @TODO 发给第三方人员
        //         if (isset($person_info['mobile'])) {

        //         }


    }

    /**
     * 提交订单短信
     */
    public function trade_order_message($order_id_encode, $store_info) {
        //         if (ONDEV) {
        //             return false;
        //         }

        $store_user_info = user_helper::get_user_info($store_info['user_id']);
        $owner_user_info = user_helper::get_user_info($store_info['owner_id']);
        $dealer_info = user_helper::get_user_info($store_info['agency_id']);

        // 给商户发短信
        $replace_arr = array(
            'order_id' => $order_id_encode,
            'store_name' => $store_info['title'] . $store_info['title_branch'],
            'dealer_name' => $dealer_info['realname'],
            'phone_num' => $dealer_info['mobile']
        );
        aliysms_helper::sms_send('抱抱堂', $replace_arr, $store_user_info['mobile'], 'store_stock_store');

        // 发推送消息以及站内信
        message_center_helper::get_config_add_message('store_stock_store', $replace_arr, $store_user_info['id'], 'store', $order_id_encode, 1, 'arrival');
        $agency_user_info = user_helper::get_user_info($store_info['agency_id']);

        // 给合伙人发短信
        $replace_arr = array(
            'order_id' => $order_id_encode,
            'store_name' => $store_info['title'] . $store_info['title_branch']
        );
        aliysms_helper::sms_send('抱抱堂', $replace_arr, $agency_user_info['mobile'], 'store_stock_dealer');

        // 发推送消息以及站内信
        message_center_helper::get_config_add_message('store_stock_dealer', $replace_arr, $agency_user_info['id'], 'manage', $order_id_encode, 1, 'arrival');

        // 如果市场人员和合伙人不是同一个人，才下发短信
        if ($store_info['marketing_id'] !== $agency_user_info['id']) {

            $marketing_user_info = user_helper::get_user_info($store_info['marketing_id']);

            // 给市场人员发短信
            $replace_arr = array(
                'order_id' => $order_id_encode,
                'store_name' => $store_info['title'] . $store_info['title_branch'],
                'name' => $marketing_user_info['realname']
            );
            aliysms_helper::sms_send('抱抱堂', $replace_arr, $marketing_user_info['mobile'], 'store_stock_marketing');

            // 发推送消息以及站内信
            message_center_helper::get_config_add_message('store_stock_marketing', $replace_arr, $marketing_user_info['id'], 'manage', $order_id_encode, 1, 'arrival');
        }
    }


    /**
     * 确认收款
     */
    public function confirm_pay_message($order_id) {
        //         if (ONDEV) {
        //             return false;
        //         }

        $order_info = order_helper::get_order_info($order_id);
        $order_no = order_helper::order_id_encode($order_id);
        $dealer_user_info = user_helper::get_user_info($order_info['user_id']);

        // 发给销售助理
        $assistant_user_info = array();
        if ($dealer_user_info['assistant_user_id']) {
            $assistant_user_info = user_helper::get_user_info($dealer_user_info['assistant_user_id']);
        }
        $assistant_name = '默认助理';
        $assistant_mobile = '13000000000';
        if ($assistant_user_info) {
            $assistant_name = $assistant_user_info['realname'];
            $assistant_mobile = $assistant_user_info['mobile'];
        }

        $msg_arr = array(
            'dealer_name' => $dealer_user_info['realname'],
            'order_id' => $order_no,
            'assistant_name' => $assistant_name,
            'phone_num' => $assistant_mobile
        );

        // 发给合伙人
        aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_user_info['mobile'], 'financial_confirm_collection_dealer');
        message_center_helper::get_config_add_message('financial_confirm_collection_dealer', $msg_arr, $dealer_user_info['id'], 'manage', $order_no, 1, 'arrival');

        // 发给销售助理
        aliysms_helper::sms_send('抱抱堂', $msg_arr, $assistant_mobile, 'financial_confirm_collection_assistant');
    }


}

?>